﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FBApp.Classes;
using Facebook;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace FBApp
{
	/// <summary>
	/// Interaction logic for Messages.xaml
	/// </summary>
	public partial class Messages : UserControl
	{
        private LoadingIndicator indicator;
        private FacebookClient fbc = null;
        
        //Feeds
        public ObservableCollection<MessageItem> messageFeed { get; set; }
        public static Dictionary<string, JSONObject> messageFeedDictionary = new Dictionary<string, JSONObject>();

		public Messages()
		{
			this.InitializeComponent();
            messageFeed = new ObservableCollection<MessageItem>();
            icItems.ItemsSource = messageFeed;     
		}

        public void setIndicator(LoadingIndicator indicator)
        {
            this.indicator = indicator;
        }

        public void setFacebookClient(FacebookClient fbc)
        {
            this.fbc = fbc;
        }

        public List<JSONObject> updateMessageFeed()
        {
            JSONObject response = fbc.getFacebookAPI().Get("/me/inbox");

            Dictionary<string, JSONObject> dictionary = response.Dictionary;

            JSONObject[] messageFeedItems = dictionary["data"].Array;

            List<JSONObject> newItems = new List<JSONObject>();

            for (int k = 0; k < messageFeedItems.Length; k++)
            {
                if (!messageFeedDictionary.ContainsKey(messageFeedItems[k].Dictionary["id"].ToDisplayableString()))
                {
                    messageFeedDictionary[messageFeedItems[k].Dictionary["id"].ToDisplayableString()] = messageFeedItems[k];
                    newItems.Add(messageFeedItems[k]);
                }
            }
           // fbc.getNotifier().displayNotification(newItems.Count + " message items added");

            return newItems;
        }

        public void addMessageItem(JSONObject item)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                string subject ="No Subject";

                if (item.Dictionary.ContainsKey("subject"))
                {
                    subject = item.Dictionary["subject"].ToDisplayableString();
                }                              

                lock (messageFeed)
                {                    
                    messageFeed.Add(new MessageItem(item.Dictionary["id"].ToDisplayableString(), item.Dictionary["from"].Dictionary["name"].ToDisplayableString(), item.Dictionary["from"].Dictionary["id"].ToDisplayableString(), item.Dictionary["to"].Dictionary["data"].Array[0].Dictionary["name"].ToDisplayableString(), subject, item.Dictionary["message"].ToDisplayableString(), item.Dictionary["updated_time"].ToDisplayableString()));
                }
            }));
        }

        public void refreshUI()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                icItems.UpdateLayout();
            }
             ));
        }

	}
}